from selenium import webdriver
class SeleniumlUtil:
    __driver = None
    __type_name = None
    __option = None

    @ classmethod
    def creat_option(cls):
        if cls.__type_name == 'Chrome':
            cls.__option = webdriver.ChromeOptions()

        if cls.__type_name == 'Firefox':
            cls.__option = webdriver.FirefoxOptions()
        if cls.__type_name == 'Ie':
            cls.__option = webdriver.IeOptions()

    @classmethod
    def remove_check(cls):
        if cls.__option is None:
            cls.creat_option()
        if cls.__type_name == 'Chrome':
            cls.__option.add_experimental_option('excludeSwitches', ['enable-automation'])
        if cls.__type_name == 'Firefox':
            pass
        if cls.__type_name == 'Ie':
            pass
        return cls.__option

    @classmethod
    def other_params(cls,para):
        if cls.__option is None:
            cls.creat_option()
        if 'proxies' in para.keys():
            # ip = '127.0.0.1:8080'
            ip = para['proxies']
            # print('当前ip：' + ip)
            cls.__option.add_argument('--proxy-server=' + ip)
            # 账号密码认证代理
            # option.add_extension(get_chrome_proxy_extension(proxy=ip))
        if 'download_path' in para.keys():
            print(para['download_path'])
            cls.__option.add_experimental_option("prefs", {
                "download.default_directory": para['download_path'],
                "download.prompt_for_download": False,
                "download.directory_upgrade": True,
                "safebrowsing.enabled": True
            })

    @classmethod
    def get(cls,type_name,url=None,remove_checks=False,params=None):
        cls.__type_name = type_name
        if remove_checks:
            cls.remove_check()
        if params:
            cls.other_params(params)
        if type_name=='Chrome':
            cls.__driver = webdriver.Chrome(chrome_options=cls. __option)
        if type_name=='Firefox':
            cls.__driver = webdriver.Firefox(firefox_options=cls. __option)
        if type_name=='Ie':
            cls.__driver = webdriver.Ie(ie_options=cls. __option)
        if url:
            cls.__driver.get(url)
        return cls
